package com.ruoyi.server.utils;

import com.ruoyi.server.enums.AddConstants5;
import com.ruoyi.server.enums.CmdConstants;
import com.ruoyi.server.person.MountUser;
import com.ruoyi.server.person.PassageWayUser;
import com.ruoyi.server.pkg.SendMsg;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;

import java.time.LocalDateTime;
import java.util.*;

/**
 * ADD 工具
 *      只是为了避免在一个类中过于繁琐
 * @author OpenACloud
 * @create 2024/6/12 17:53
 */
public class AddUtil {


    /**
     * 申请挂载好友申请
     */
    public static AttributeKey<HashSet<Long>> ADD_APPLICATION_FRIEND = AttributeKey.valueOf("ADD_APPLICATION_FRIEND");

    /**
     * 申请挂载好友申请
     */
    public static AttributeKey<HashMap<Long, Set<Long>>> ADD_APPLICATION_GROUP = AttributeKey.valueOf("ADD_APPLICATION_FRIEND");


    /**
     * 生成回执消息
     *
     * @param CMD
     */
    public static SendMsg produceReplyMsg(byte CMD, Long SENDID){
        SendMsg replyMsg = new SendMsg();
        replyMsg.CMD = CMD;
        replyMsg.SENDID = SENDID;
        replyMsg.TIME = LocalDateTime.now();
        return replyMsg;
    }

    /**
     *  添加Friend快捷工具
     *
     * @param ctx
     * @param sendMsg
     * @param replyMsg
     */
    public static boolean addFriend(ChannelHandlerContext ctx, SendMsg sendMsg, SendMsg replyMsg){
        Long receiveId = sendMsg.RECEIVEID;
        Set<Long> userIds = ctx.attr(OnlineUtil.USER_FRIEND).get();
        if (userIds.contains(receiveId)) {
            replyMsg.STATUS = AddConstants5.ADD_FAIL;
            replyMsg.CONTEXT = "好友已存在, 请勿重复添加! ";
            ctx.channel().writeAndFlush(replyMsg);
            return false;
        }else {
            replyMsg.STATUS = AddConstants5.ADD_WAIT;
            replyMsg.CONTEXT = "申请添加成功, 请等待申请";
            ctx.channel().writeAndFlush(replyMsg);
            ChannelHandlerContext userCtx = OnlineUtil.ONLINE_MAPS.get(receiveId);
            if (Objects.nonNull(userCtx)) {
                PassageWayUser wayUser = ctx.attr(OnlineUtil.PASSAGE_WAR_USER).get();

                MountUser mountUser = new MountUser();
                mountUser.setUserId(wayUser.getUserId());
                mountUser.setUserName(mountUser.getUserName());

                SendMsg receiveMsg = new SendMsg();
                receiveMsg.CMD = AddConstants5.ADD_FRIEND;
                receiveMsg.STATUS = AddConstants5.ADD_APPLICATION;
                receiveMsg.SENDID = sendMsg.SENDID;
                receiveMsg.MOUNTUSER = mountUser;
                receiveMsg.TIME = LocalDateTime.now();
                userCtx.channel().writeAndFlush(receiveMsg);
                return false;
            }
            return true;
        }
    }



    /**
     *  添加Group快捷工具
     *
     * @param ctx
     * @param sendMsg
     * @param replyMsg
     */
    public static Set<Long> addGroup(ChannelHandlerContext ctx, SendMsg sendMsg, SendMsg replyMsg, Set<Long> adminGroup)
    {

        Set<Long> apSaveUser = new HashSet<>(adminGroup.size());
        Long apGroupId = sendMsg.RECEIVEID;
        Set<Long> userGroups = ctx.attr(OnlineUtil.USER_GROUP).get();
        if (userGroups.contains(apGroupId)) {
            replyMsg.STATUS = AddConstants5.ADD_FAIL;
            replyMsg.CONTEXT = "请勿重复添加群聊!!!!!";
            ctx.channel().writeAndFlush(replyMsg);
        }else {
            for (Long userGroup : adminGroup) {
                ChannelHandlerContext userCtx = OnlineUtil.ONLINE_MAPS.get(userGroup);
                if (Objects.nonNull(userCtx)) {
                    PassageWayUser wayUser = ctx.attr(OnlineUtil.PASSAGE_WAR_USER).get();

                    MountUser mountUser = new MountUser();
                    mountUser.setUserId(wayUser.getUserId());
                    mountUser.setUserName(mountUser.getUserName());

                    SendMsg receiveMsg = new SendMsg();
                    receiveMsg.CMD = AddConstants5.ADD_GROUP;
                    receiveMsg.STATUS = AddConstants5.ADD_APPLICATION;
                    receiveMsg.SENDID = sendMsg.SENDID;
                    receiveMsg.MOUNTUSER = mountUser;
                    receiveMsg.TIME = LocalDateTime.now();
                    userCtx.channel().writeAndFlush(receiveMsg);

                }else {
                    apSaveUser.add(userGroup);
                }
            }
            replyMsg.STATUS = AddConstants5.ADD_WAIT;
            replyMsg.CONTEXT = "申请添加成功, 请等待申请";
            ctx.channel().writeAndFlush(replyMsg);
        }
        return apSaveUser;
    }
}
